package com.jieshuibao.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jieshuibao.common.res.BaseRes;
import com.jieshuibao.model.db.Rotation;
import com.jieshuibao.model.db.RotationExample;
import com.jieshuibao.model.db.RotationExample.Criteria;
import com.jieshuibao.model.domain.RotationsResponse;
import com.jieshuibao.service.RedisService;
import com.jieshuibao.service.RotationService;

@RequestMapping(value = "/rotation")
@Controller
public class RotationController {
	private static final Logger logger = LoggerFactory
			.getLogger("RotationController.class");
	private static final ObjectMapper mapper = new ObjectMapper();

	@Resource
	private RedisService redisService;
	@Resource
	private RotationService rotationService;
	@Value("${rotation.images.url}")
	private String rotationImagesUrl;
	@RequestMapping(value = "/display", method = RequestMethod.POST)
	@ResponseBody
	public Object homeRotation(HttpServletRequest request) {
		RotationsResponse rotationsResponse = new RotationsResponse();

		try {
			RotationExample example = new RotationExample();
			example.setOrderByClause("orderNum");
			Criteria createCriteria = example.createCriteria();
			createCriteria.andStatusEqualTo("1");
			createCriteria.andTypeEqualTo("0");//首页轮播图
			List<Rotation> rotations = rotationService.getRotations(example);
			rotationsResponse.setCode(BaseRes.SUCCESS_CODE);
			rotationsResponse.setRotations(rotations);
			rotationsResponse.setRotationImageUrl(rotationImagesUrl);
			logger.info("返回：" + mapper.writeValueAsString(rotationsResponse));
		} catch (Exception e) {
			e.printStackTrace();
			rotationsResponse.setCode(BaseRes.FAIL_CODE);
			rotationsResponse.setDes(BaseRes.FAIL_DES);
		}
		return rotationsResponse;
	}
	
	@RequestMapping(value = "/article", method = RequestMethod.POST)
	@ResponseBody
	public Object articleRotation(HttpServletRequest request) {
		RotationsResponse rotationsResponse = new RotationsResponse();

		try {
			RotationExample example = new RotationExample();
			example.setOrderByClause("orderNum");
			Criteria createCriteria = example.createCriteria();
			createCriteria.andStatusEqualTo("1");
			createCriteria.andTypeEqualTo("1");//文章轮播图
			List<Rotation> rotations = rotationService.getRotations(example);
			rotationsResponse.setCode(BaseRes.SUCCESS_CODE);
			rotationsResponse.setRotations(rotations);
			rotationsResponse.setRotationImageUrl(rotationImagesUrl);
			logger.info("返回：" + mapper.writeValueAsString(rotationsResponse));
		} catch (Exception e) {
			e.printStackTrace();
			rotationsResponse.setCode(BaseRes.FAIL_CODE);
			rotationsResponse.setDes(BaseRes.FAIL_DES);
		}
		return rotationsResponse;
	}

}
